package com.tommy.jiami.utils.task;

import android.content.*;
import android.database.*;
import android.os.*;
import com.tommy.jiami.utils.*;
import java.io.*;
import java.util.*;
import android.provider.MediaStore.*;
import com.tommy.jiami.view.*;
import android.graphics.*;
import android.media.*;
import com.tommy.jiami.*;
import android.app.*;
import android.util.*;
import android.net.*;

public abstract class GetMediaTask extends Task<Integer,List<Media>>
{
	public GetMediaTask(Context context)
	{
		super(context,"获取内容中...");
	}
	
	@Override
	protected List<Media> doInBackground(Integer[] params)
	{
		List<Media> medias=new ArrayList<Media>();
		Media media;int mediaTypeId=params[0];
		if (mediaTypeId != MediaView.TYPE_ENCRYPTED)
		{
			Uri uri;
			if (mediaTypeId == MediaView.TYPE_PICTURE)
				uri = Images.Media.EXTERNAL_CONTENT_URI;
			else if (mediaTypeId == MediaView.TYPE_AUDIO)
				uri= Audio.Media.EXTERNAL_CONTENT_URI;
			else uri = Video.Media.EXTERNAL_CONTENT_URI;
			Cursor c=context.getContentResolver().query(uri, null, null, null, null);

			while (c.moveToNext())
			{
				media = new Media();
				int id=c.getInt(c.getColumnIndex("_id"));
				media.setType(mediaTypeId);
				media.setFromUri(Uri.parse(uri+"/"+id));
				String path=c.getString(c.getColumnIndex("_data"));
				media.setFile(new File(path));
				media.setName(media.getFile().getName());
				Bitmap thumbnail;		
				if (mediaTypeId == MediaView.TYPE_PICTURE)
					thumbnail = readThumbnail(path);
				else if (mediaTypeId == MediaView.TYPE_AUDIO)
					thumbnail = BitmapFactory.decodeResource(context.getResources(), R.drawable.audio);
				else thumbnail = ThumbnailUtils.createVideoThumbnail(path, Video.Thumbnails.MINI_KIND);
				media.setThumbnail(thumbnail);
				medias.add(media);
			}
		}else{
			try
			{
				File directory=new File(context.getFilesDir() + File.separator + "jiamis");
				String userPwd=Utils.getUserPassword();
				for (File file:directory.listFiles())
				{
					if (file.isFile())
					{
						media = new Media();
						String path=Crypt.decryptString(file.getName(), userPwd);
						media.setFile(file);
						media.setName(Utils.getFileName(path));
						int resId;
						InputStream in=new FileInputStream(file);
						
						if(Utils.isPicture(path)){
							byte[] src=new byte[in.available()];
							in.read(src);
							byte[] data=Crypt.decrypt(src,userPwd);
							media.setThumbnail(readThumbnail(data));
						}else{
							if(Utils.isAudio(path)){
								resId=R.drawable.audio;
							}else if(Utils.isVideo(path)){
								resId=R.drawable.video;
							}else{
								resId=R.drawable.ic_launcher;
							}
							media.setThumbnail(BitmapFactory.decodeResource(context.getResources(),resId));
						}
						medias.add(media);
					}
				}
			}
			catch (Exception e)
			{}
		}
		
		return medias;
	}
	
	private Bitmap readThumbnail(String path)
	{
		Bitmap thumbnail=null;
		try
		{
			InputStream input=new FileInputStream(path);
			byte[] buf=new byte[input.available()];
			input.read(buf);
			thumbnail=readThumbnail(buf);
			input.close();
		}
		catch (IOException e)
		{Log.e("AndroidRuntime",e.toString());}
		return thumbnail;
	}

	private Bitmap readThumbnail(byte[] buf){
		Bitmap thumbnail=null;
		BitmapFactory.Options options=new BitmapFactory.Options();
		options.inJustDecodeBounds=true;
		BitmapFactory.decodeByteArray(buf,0,buf.length,options);
		options.inJustDecodeBounds=false;
		options.inSampleSize=Utils.computeSampleSize(options,-1,128*128);
		thumbnail=BitmapFactory.decodeByteArray(buf,0,buf.length,options);
		return thumbnail;
	}
	
	
}
